<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\course\Order;
use app\admin\model\Third;
use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use app\common\model\MoneyLog;
use app\common\model\order\PayLog;
use fast\Random;
use think\Config;
use think\Db;
use think\Validate;

/**
 * 老师个人信息接口
 * @ApiWeigh (80)
 */
class Teacher extends Api
{
    protected $noNeedLogin = ['login'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();


    }



    /**
     * 老师登录
     *
     * @ApiMethod (POST)
     * @ApiWeigh (100)
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password,2);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }


    /**
     * 获取老师详情
     * @ApiMethod   (POST)
     * @ApiWeigh (99)
     *
     * @ApiReturnParams   (name="nickname", type="string", required=true, description="姓名")
     * @ApiReturnParams   (name="avatar", type="string", required=true, description="头像")
     * @ApiReturnParams   (name="bio", type="string", required=true, description="介绍")
     * @ApiReturnParams   (name="works_images", type="string", required=true, description="作品")
     * @ApiReturnParams   (name="honor_images", type="string", required=true, description="荣誉证书")
     */
    public function getTeacherInfo(){
        $userModel = new \app\common\model\User();
        try {
            $condition = [
                "id" => $this->auth->id,
            ];
            $info = $userModel->getTeacherInfo($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功", $info);
    }


    /**
     * 修改老师信息
     *
     * @ApiMethod (POST)
     * @param string $avatar   头像地址
     * @param string $nickname 昵称
     * @param string $bio      个人简介
     * @param string $works_images      作品英文逗号隔开
     * @param string $honor_images      荣誉证书英文逗号隔开
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $nickname = $this->request->post('nickname');
        $bio = $this->request->post('bio');
        $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
        $works_images = $this->request->post('works_images');
        $honor_images = $this->request->post('honor_images');
        if ($nickname) {
            $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
            if ($exists) {
                $this->error(__('Nickname already exists'));
            }
            $user->nickname = $nickname;
        }
        if($bio){
            $user->bio = $bio;
        }
        if($avatar){
            $user->avatar = $avatar;
        }
        if($works_images){
            $user->works_images = $works_images;
        }
        if($honor_images){
            $user->honor_images = $honor_images;
        }

        $user->save();
        $this->success("保存成功");
    }
}
